﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BTCe;
using BTCe.Constant;
using BTCe.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace BTCe.Common
{
    public class WalletComm
    {
        WalletInfo _WalletInfo;
        WalletDeposit _WalletDeposit;
        BTCeAccessInfo _AccessInfo;

        public WalletComm()
        {
            _WalletInfo = new WalletInfo();
            _WalletDeposit = new WalletDeposit();
        }

        public WalletComm(BTCeAccessInfo AccessInfo)
        {
            _AccessInfo = AccessInfo;
            _WalletInfo = new WalletInfo();
            _WalletDeposit = new WalletDeposit();
        }

        public WalletDeposit GetWalletDeposit()
        {
            return _WalletDeposit;
        }

        public WalletInfo GetWalletInfo()
        {
            return _WalletInfo;
        }

        public void RetrieveWallet()
        {
            if (!_AccessInfo.IsLogin())
                return;

            string strData = "";
            string strURL;
            string strParameter;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("method", "getInfo");

            strURL = _AccessInfo.GetBaseURL() + BTCeURL.URL_WALLET;
            strParameter = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameter, _AccessInfo.GetHttpHeaderAPI(strParameter));

            SetWalletInfo(strData);

        }

        private void SetWalletInfo(string strData)
        {
            JObject objData;

            if (strData.Length == 0)
                return;

            objData = JObject.Parse(strData);


            ///"{\"success\":1,\"return\":{\"funds\":{\"usd\":186.12728781,\"btc\":3.47421438,\"ltc\":0,\"nmc\":0,\"rur\":32.40845593,\"eur\":0,\"nvc\":0,\"trc\":0,\"ppc\":0,\"ftc\":0,\"xpm\":0,\"cnh\":0,\"gbp\":0},\"rights\":{\"info\":1,\"trade\":1,\"withdraw\":0},\"transaction_count\":0,\"open_orders\":3,\"server_time\":1426841537}}"
            JToken objToken;


            //계좌 잔고 정보를 추출
            objToken = objData["return"];
            objToken = objToken["funds"];

            try
            {
                _WalletDeposit.Total_USD = Double.Parse(objToken["usd"].ToString());
                _WalletDeposit.Total_BTC = Double.Parse(objToken["btc"].ToString());
                _WalletDeposit.Total_LTC = Double.Parse(objToken["ltc"].ToString());
                _WalletDeposit.Total_NMC = Double.Parse(objToken["nmc"].ToString());
                _WalletDeposit.Total_RUR = Double.Parse(objToken["rur"].ToString());
                _WalletDeposit.Total_EUR = Double.Parse(objToken["eur"].ToString());                    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
           
        }

    }


}
